package com.orangelife.mobile.lookRepair.activity;

import java.util.HashMap;
import java.util.Map;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.curry.android.http.HttpUtil;
import com.curry.android.util.DialogHelper;
import com.curry.android.util.IntentHelper;
import com.curry.android.util.JSONHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.app.application.ExitApplication;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.common.view.ConfirmDialog;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.util.DateUtil;
import com.orangelife.mobile.util.ToastHelper;

public class GrapSingle2Activity extends OrangeLifeBaseActivity {
	
	/**订单编号*/
	private TextView tvOrderNumber;
	/**订单编号旁边的状态*/
	private TextView tvOrderStatus;
	/**地址*/
	private TextView tvAddress;
	/**报修问题*/
	private LinearLayout llRepairProblem;
	/**下单点击*/
	private LinearLayout llPlaceOrder;
	/**取消订单按钮*/
	private Button btnCancelOrder;
	/**标题中间*/
	private TextView tvTitle;
	/**下单后面的提示*/
	private TextView tvContent;
	/**标题右边*/
	private TextView tvTitleRight;
	/**接收数据*/
	private Map<String,Object> map;
	private Dialog dialog;
	/**对话框中的内容*/
	private String dialogMessage;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_grap_single2);
		initeView();
		findView();
	}
	
	/**
	 * 初始化
	 */
	private void initeView(){
		tvTitle = (TextView)findViewById(R.id.tv_title);
		tvTitleRight = (TextView)findViewById(R.id.tv_title_right);
		tvOrderNumber = (TextView)findViewById(R.id.tvOrderNumber);
		tvOrderStatus = (TextView)findViewById(R.id.tvOrderStatus);
		tvAddress = (TextView)findViewById(R.id.tvAddress);
		tvContent = (TextView)findViewById(R.id.tvContent);
		llRepairProblem = (LinearLayout)findViewById(R.id.llRepairProblem);
		llPlaceOrder = (LinearLayout)findViewById(R.id.llPlaceOrder);
		btnCancelOrder = (Button)findViewById(R.id.btnCancelOrder);
	}
	
	private void findView(){
		tvTitle.setText(R.string.order_detail);
		tvTitleRight.setVisibility(View.INVISIBLE);
		llRepairProblem.setOnClickListener(listener);
		btnCancelOrder.setOnClickListener(listener);
		llPlaceOrder.setOnClickListener(listener);
		dialog = DialogHelper.getInstance().createLoadingDialog(
				GrapSingle2Activity.this, Constant.LOADING);
		dialog.show();
		getDetail();
	}
	
	/**
	 * 监听器
	 */
	private OnClickListener listener = new OnClickListener() {
		
		@Override
		public void onClick(View view) {
			switch (view.getId()) {
			case R.id.llRepairProblem:
				if(map != null){
					String repairID =  map.get("invcID").toString();
					Intent intent = new Intent();
					intent.putExtra("lookDetail", 0);
					intent.putExtra("followID", repairID);
					intent.setClass(GrapSingle2Activity.this, RepairProblemActivity.class);
					startActivity(intent);
				}
				break;
			case R.id.btnCancelOrder:
				if(map != null){
					dialogMessage = getResources().getString(R.string.sure_cancel_order);
					isOrderDialog();
				}
				break;
			case R.id.llPlaceOrder:
				Intent intent = new Intent();
				intent.putExtra("repairCode", map.get("repaireCode").toString());
				intent.setClass(GrapSingle2Activity.this, PlaceOrderDetailActivity.class);
				startActivity(intent);
				break;
			default:
				break;
			}
		}
	};
	
	/**
	 * 弹出对话框
	 */
	private void isOrderDialog() {
		final ConfirmDialog dialog = new ConfirmDialog(GrapSingle2Activity.this, "温馨提示",dialogMessage, "返回", "取消订单");
		dialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
			@Override
			public void doLeft() {
				dialog.dismiss();
			}

			@Override
			public void doRight() {
				dialog.dismiss();
				//取消订单
				cancelOrder();
				return;
			}
		});

		dialog.show();
	}
	private void isOrderDialog1() {
		Builder builder = new Builder(GrapSingle2Activity.this);
		builder.setMessage(dialogMessage);
		builder.setPositiveButton("取消订单", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				//取消订单
				cancelOrder();
				return;
			}
		});
		builder.setNegativeButton("返回", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.create().show();
	}
	
	/**
	 * 设置详情
	 */
	private void setDetail() {
		tvOrderNumber.setText(map.get("repaireCode").toString());
		tvOrderStatus.setText("待抢单");
		tvAddress.setText(map.get("address").toString());
		String time  = DateUtil.getInstance().tranStringForDate_11(map.get("tscreate").toString());
		tvContent.setText("下单:"+time);
	}
	/**
	 * 设置用户已读
	 */
	private void setRead(){
		String repairID = "";
		if(map != null){
			repairID = map.get("invcRepaireID").toString();
		}else{
			Intent intent = getIntent();
			repairID = intent.getStringExtra("jumpuri");
		}
		Map<String, Object> mapSubmit = new HashMap<String, Object>();
		mapSubmit.put("invcRepaireID", repairID);

		JSONRequest.getInstance().sendRequest(JSONRequest.PUT, Constant.URL_READED, mapSubmit, handler, 6, HttpUtil.ACTION_JSON);
		
	}
	
	/**
	 * 获得详情
	 * 
	 */
	private void getDetail() {
		String repairID = "";
		Intent intent = getIntent();
		repairID = intent.getStringExtra("followID");
		Map<String, Object> mapSubmit = new HashMap<String, Object>();
		mapSubmit.put("{repairID}", repairID);
		Map<String, Object> rootMap = new HashMap<String, Object>();
		rootMap.put("map", mapSubmit);
		rootMap.put("url", Constant.URL_PROBLEM);
		rootMap.put("wat", 9);
		new JSONRequest(JSONRequest.GET, rootMap, handler, HttpUtil.ACTION_URI);
	}
	/**
	 * 取消订单
	 * 
	 */
	private void cancelOrder() {
		Intent intent = getIntent();
		String repairID = "";
		if(map!=null&&map.size()>0){
			repairID = map.get("invcID").toString();
			Map<String, Object> mapSubmit = new HashMap<String, Object>();
			mapSubmit.put("invoiceID", repairID);
			mapSubmit.put("repaireSubStatus", 0);
			Map<String, Object> rootMap = new HashMap<String, Object>();
			rootMap.put("map", mapSubmit);
			rootMap.put("url", Constant.URL_CANCEL_ORDER);
			rootMap.put("wat", 7);
			new JSONRequest(JSONRequest.PUT, rootMap, handler, HttpUtil.ACTION_JSON);
		}
	}
	
	/**
	 * 处理返回的信息数据
	 */
	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			DialogHelper.closeDialog(dialog);
			switch (msg.what) {
			case 9:
				// list = new ArrayList<Map<String,Object>>();
				map = new HashMap<String, Object>();
				if(!JSONHelper.jsonToMap(
						msg.obj.toString()).get("entity").equals("")){
					map = (Map<String, Object>) JSONHelper.jsonToMap(
							msg.obj.toString()).get("entity");
					DialogHelper.closeDialog(dialog);
					// list.add(map);
					setDetail();
				}
				break;
			case 7:
				String errInfo = JSONHelper.jsonToMap(msg.obj.toString())
				.get("errInfo").toString();
				if (errInfo.equals("OK")){
					DialogHelper.closeDialog(dialog);
					ToastHelper.getInstance()._toast(getResources().getString(R.string.cancel_success));
					IntentHelper.getInstances().getIntent(GrapSingle2Activity.this, LookForRepairListActivity.class);
				    ExitApplication.getInstance().finishPaidActivity();
					finish();
				}else{
					DialogHelper.closeDialog(dialog);
					ToastHelper.getInstance()._toast(
							getResources().getString(
									R.string.cancel_fail));
				}
				break;
			case 1011:
				isLogin(dialog);
				break;
			case 500:
				ToastHelper.getInstance()._toast(
						msg.obj.toString());
				break;
			default:
				break;
			}
		}

	};

}
